package com.ybu;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

public class ArrayListDemo {
    public static  void main(String[] args){
        ArrayList<String> list = new ArrayList<String>();
        //添加
        list.add("zhangsan");
        list.add(0,"lisi");
        //获取
        String s = list.get(1);
        System.out.println(s);
        //修改
        list.set(0,"wangwu");
        list.remove(1);
        System.out.println(list);
        list.add("zhaoliu");
        list.add("zhaoliu");

        System.out.println("-------------------集合的遍历方法1---------------------");
        //for循环遍历
        for(int i=0;i<list.size();i++){
            String obj = list.get(i);
            System.out.println(obj);
        }
        System.out.println("-------------------集合的遍历方法2---------------------");
        //增强for循环遍历
        for(String obj:list){
            System.out.println(obj);
        }
        //迭代器遍历
        System.out.println("-------------------集合的遍历方法3---------------------");
        //获取集合的迭代器对象
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next);
        }
        //Lambda表达式
        System.out.println("-------------------集合的遍历方法4---------------------");
        //lambda表达式本质是一个函数，Consumer<String> consumer
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.forEach(name-> System.out.println(name));
        list.forEach(System.out::println);
        }

    }

